﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using PSE.Hosting.ServiceHoster.Logging;
using PSE.Hosting.ServiceHoster.Business;
using System.Reflection;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;

namespace PSE.Hosting.ServiceHoster.Process
{
    /// <summary>
    /// Serviço do Hoster Process
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
    public class ServiceHosterProcessService : IServiceHosterProcessService
    {
        private Services _services;
        private string _packageName;

        public ServiceHosterProcessService()
        {

        }

        public ServiceHosterProcessService(string packageName)
        {
            _services = Services.Instance;
            _packageName = packageName;
        }

        /// <summary>
        /// Verifica se o processo está ativo
        /// </summary>
        /// <returns>True se o processo estive ativo</returns>
        public bool IsAlive()
        {
            return true;
        }

        /// <summary>
        /// Fecha o processo
        /// </summary>
        public void CloseProcess()
        {
            Program.Exit();
        }

        /// <summary>
        /// Start all services a specified package.
        /// </summary>
        /// <param name="packageName">The package name to start the services.</param>
        /// <param name="initialize">Initialize the assenblies.</param>
        public List<MessageData> StartAll(bool initialize)
        {
            return _services.StartAll(_packageName, initialize);
        }

        /// <summary>
        /// Stop all the services from an assembly.
        /// </summary>
        /// <param name="packageName">The name of the package containing the assembly to be stopped.</param>
        /// <param name="codeBase">The name of the assembly to be stopped.</param>
        public void StopAssembly(string codeBase)
        {
            _services.StopAssembly(_packageName, codeBase);
        }

        /// <summary>
        /// Start all WCF services from a specific assembly.
        /// </summary>
        /// <param name="packageName">The name of the package of the assembly to start.</param>
        /// <param name="fileName">The name of the assembly file to start. This is the filename only, without the path.</param>
        /// 
        public List<MessageData> StartAssembly(string fileName)
        {
            return _services.StartAssembly(_packageName, fileName);
        }

        /// <summary>
        /// Starts a Service configured within the Hoster app.config.
        /// </summary>
        /// <param name="assembly">The assembly name.</param>
        /// <param name="serviceName">The service name to start.</param>
        public void StartService(string assembly, string serviceName)
        {
            _services.StartService(_packageName, assembly, serviceName);
        }

        /// <summary>
        /// Stops the specified service.
        /// </summary>
        /// <param name="packageName">The name of the package containing the service.</param>
        /// <param name="assembly">The assembly name.</param>
        /// <param name="serviceName">The service name to stop.</param>
        public void StopService(string assembly, string serviceName)
        {
            _services.StopService(_packageName, assembly, serviceName);
        }

        /// <summary>
        /// Stops all the services already started from a specific package.
        /// </summary>
        public void StopAll()
        {
            _services.StopAll(_packageName);
        }

        /// <summary>
        /// Gets a list of all the services configured within the Service Hoster app.config.
        /// </summary>
        /// <returns></returns>
        public List<Entities.Service> GetServiceList(string assembly)
        {
            return _services.GetServiceList(_packageName, assembly);
        }

        /// <summary>
        /// Returns a list of all the assemblies added in a package.
        /// </summary>
        /// <param name="packageName">The name of the package to get the list of assenblies.</param>
        /// <returns>List of added assemblies.</returns>
        public List<Entities.Assembly> GetAssemblyList()
        {
            return _services.GetAssemblyList(_packageName);
        }

        /// <summary>
        /// Adds a Service to the Service Hoster config file.
        /// </summary>
        /// <param name="packageName">The name of the package.</param>
        /// <param name="codeBase">The assembly code base.</param>        
        public void AddAssembly(string codeBase)
        {
            _services.AddAssembly(_packageName, codeBase);
        }

        /// <summary>
        /// Removes a Service from the Service Hoster config file.
        /// </summary>
        /// <param name="packageName">The name of the package.</param>
        /// <param name="assemblyFile">The name of the assembly file (without path).</param>
        public void RemoveAssembly(string assemblyFile)
        {
            _services.RemoveAssembly(_packageName, assemblyFile);
        }
    }
}
